package newtifyme.server;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.google.gdata.client.youtube.YouTubeQuery;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.VideoFeed;
import com.google.gdata.data.youtube.YouTubeMediaGroup;
import com.google.gdata.data.youtube.YtPublicationState;
import com.google.gdata.util.ServiceException;
public class HttpServerSample {
	
  public static void main(String[] args) throws IOException {
    InetSocketAddress addr = new InetSocketAddress(8080);
    HttpServer server = HttpServer.create(addr, 0);

    HttpContext context = server.createContext("/init", new MyHandler());
    
    server.setExecutor(Executors.newCachedThreadPool());
    server.start();
    System.out.println("Server is listening on port 8080" );
  }
}

class MyHandler implements HttpHandler {
	static HashMap <String,SearchTimer> idkeyMap= new HashMap();
	private String requestMethod;
	private Headers responseHeaders;
	private OutputStream responseBody;
	private HttpExchange exchange;
  public void handle(HttpExchange exchange) throws IOException {
	  this.exchange = exchange;
	  System.out.println(exchange.getRemoteAddress().toString());
    requestMethod = exchange.getRequestMethod();
    if (requestMethod.equalsIgnoreCase("GET")) {
      //Headers responseHeaders = exchange.getResponseHeaders();
    	responseHeaders = exchange.getResponseHeaders();
      responseHeaders.set("Content-Type", "text/plain");
      exchange.sendResponseHeaders(200, 0);
      //System.out.println(exchange.toString());
      //OutputStream responseBody = exchange.getResponseBody();
      responseBody = exchange.getResponseBody();
      
     
      
      String URI = exchange.getRequestURI().toASCIIString();
      parser(URI.substring(6,URI.length()));

      responseBody.close();
    }
  }
  public void parser(String msg) {
  	String[] message = msg.split("&");
  	System.out.println(msg);
  	//System.out.println(message[0]);
  	if (message[0].startsWith("uid")) {
  		SearchTimer searchTimer;
  		String uid = message[0].substring(4,message[0].length());
  			String action = message[1];
  			String keyword = message[2];
  			if (action.equals("init")&&!idkeyMap.containsKey(uid+keyword)) {
  				int interval = Integer.parseInt(message[3]);
  				searchTimer = new SearchTimer(interval,keyword,uid);
  				idkeyMap.put(uid+keyword,searchTimer);
  				searchTimer.put(exchange);
  				searchTimer.start();
  				
  				 Headers requestHeaders = exchange.getRequestHeaders();
  			      
  			      Set<String> keySet = requestHeaders.keySet();
  			      Iterator<String> iter = keySet.iterator();
  			      try {
					responseBody.write(((String)"None").getBytes());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
  			} 
  			if (action.equals("cancel")){
  				idkeyMap.get(uid+keyword).timer.cancel();
  				idkeyMap.remove(uid+keyword);
  			}
  			System.out.println("user "  + uid);
  			if (action.equals("")) {
  				
  			}
  	}
 
  
  
  }
}